package com.mall.controller;

import com.mall.domain.Permission;
import com.mall.domain.Role;
import com.mall.domain.User;
import com.mall.service.impl.PermissionServiceImpl;
import com.mall.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色web层
 * @author : liangY
 * @date : 2021-01-09 11:12
 */
@RequestMapping("/roles")
@Controller
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;
    @Autowired
    private PermissionServiceImpl permissionService;

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(){
        Map<String,Object> roleMap = new HashMap<>();
        //获取角色集合
        List<Role> roles = roleService.findAll();
        //放入Map集合
        roleMap.put("data",roles);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取角色列表成功");
        metaMap.put("status",200);

        roleMap.put("meta",metaMap);
        return roleMap;
    }

    /**
     * 删除角色权限
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> delById(HttpServletRequest request){
        Integer role_id = Integer.parseInt(request.getParameter("role_id"));
        Integer permission_id = Integer.parseInt(request.getParameter("permission_id"));
        Map<String,Object> roleMap = new HashMap<>();
        Integer delFlag = roleService.delById(role_id,permission_id);
        //获取角色集合
        List<Permission> permissions = permissionService.getTree2(role_id);
        //放入Map集合
        roleMap.put("data",permissions);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","删除指定角色权限成功");
        metaMap.put("status",200);

        roleMap.put("meta",metaMap);
        return roleMap;
    }

    /**
     * 查询指定角色权限
     * @return
     */
    @RequestMapping("/findOne")
    public @ResponseBody
    Map<String, Object> findOne(HttpServletRequest request){
//        Integer role_id = Integer.parseInt(request.getParameter("role_id"));
        Map<String,Object> roleMap = new HashMap<>();
        //获取角色集合
        List<Permission> permissions = permissionService.getTree();
        //放入Map集合
        roleMap.put("data",permissions);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取该角色指定权限成功");
        metaMap.put("status",200);

        roleMap.put("meta",metaMap);
        return roleMap;
    }

    /**
     * 更新指定角色
     * @param role
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody Role role){
        Map<String,Object> roleMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("role",role);
        //放入Map集合
        roleMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = roleService.update(role);
        if(flag > 0){
            metaMap.put("msg","更新角色成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新角色失败");
            metaMap.put("status",0);
        }
        roleMap.put("meta",metaMap);
        return roleMap;
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody Role role){
        Map<String,Object> roleMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("role",role);
        //放入Map集合
        roleMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = roleService.add(role);
        if(flag > 0){
            metaMap.put("msg","添加角色成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","添加角色失败");
            metaMap.put("status",0);
        }
        roleMap.put("meta",metaMap);
        return roleMap;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delRole")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> roleMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        roleMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = roleService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除角色成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除用户失败");
            metaMap.put("status",0);
        }
        roleMap.put("meta",metaMap);
        return roleMap;
    }

    /**
     * 添加角色权限
     * @param request
     * @return
     */
    @RequestMapping("/setPermissions")
    public @ResponseBody
    Map<String, Object> setPermissions(HttpServletRequest request){
        //要添加权限的角色Id
        Integer roleId = Integer.parseInt(request.getParameter("roleId"));
        //前端传来的权限id字符串，以,隔开
        String pids = request.getParameter("pids");
        //先清空指定角色权限
        roleService.delByRoleId(roleId);
        //是否添加成功标识
        int flag = 0;
        //将字符串转化为字符数组
        String pid[] = pids.split(",");
        for (int i = 0; i < pid.length; i++) {
            if(Integer.parseInt(pid[i]) <= 100 ){
                //为角色添加指定权限
                flag = roleService.addById(roleId,Integer.parseInt(pid[i]));
            }
        }
        Map<String,Object> Map = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        Map.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        if(flag > 0){
            metaMap.put("msg","添加角色权限成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","添加角色权限失败");
            metaMap.put("status",0);
        }
        Map.put("meta",metaMap);
        return Map;
    }

}
